﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Interface.Models;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace CNKI.TPI.Web.Interface.Areas.Resource.Controllers
{
    public class InsertDocumentController : BaseApiController
    {
        private ISearchService searchService = null;

        public InsertDocumentController(ISearchService searchService)
        {
            this.searchService = generator.CreateInterfaceProxyWithTarget<ISearchService>(searchService, new IInterceptor[] { new LogInterceptor() });
        }

        // GET api/insert/5
        //public object Get(string dbcode, string data)
        //{
        //    OutputEntity result = new OutputEntity();
        //    try
        //    {
        //        IList<FieldInfo> fieldList = null;
        //        if (!string.IsNullOrEmpty(data))
        //        {
        //            fieldList = SerializeHelper.Deserialize<IList<FieldInfo>>(data);
        //        }
        //        bool status = searchService.InsertMetaData(dbcode, fieldList);
        //        if (status)
        //        {
        //            result.Status = Status.Success;
        //        }
        //        else
        //        {
        //            result.Status = Status.Error;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LogOpr.Error(ex.ToString());
        //        result.Status = Status.Error;
        //    }
        //    return result;
        //}

        // POST api/insert
        [ValidateInput(false)]
        public object Post()
        {
            OutputEntity result = new OutputEntity();
            try
            {
                HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context     
                HttpRequestBase request = context.Request;//定义传统request对象
                string dbcode = request.Form["dbcode"];
                string data = request.Form["data"];

                Dictionary<string, object> fieldList = null;
                if (!string.IsNullOrEmpty(data))
                {
                    fieldList = SerializeHelper.Deserialize<Dictionary<string, object>>(data);
                }
                int status = searchService.InsertMetaData(dbcode, fieldList);
                if (status > 0)
                {
                    result.Status = Status.Success;
                }
                else
                {
                    result.Status = Status.Error;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result.Status = Status.Error;
                result.message = ex.Message;
            }
            return result;
        }

    }
}
